﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;

namespace GE_Management_Tool.Planning
{
    public partial class frmA0016_Assign_Artist : frmBase
    {
        IList<int> m_lsAsset_ID;
        IList<CAsset> m_lsAsset;
        IList<CProject_Staff> m_lsStaff;

        public frmA0016_Assign_Artist()
        {
            InitializeComponent();
            CCommon_Function.Format_All(this);
        }

        public void Show_To_Display(IList<int> p_lsAsset_ID)
        {
            m_lsAsset_ID = p_lsAsset_ID;
            this.ShowDialog();
        }

        private void frmA0016_Assign_Asset_To_Artist_Load(object sender, EventArgs e)
        {
            try
            {
                //load toàn bộ danh sách các asset sẽ thực hiện:
                m_lsAsset = new List<CAsset>();
                CAsset_Controller v_objCtrlAsset = new CAsset_Controller();
                double v_dbTotal_Manday = 0;
                foreach (int v_i in m_lsAsset_ID)
                {
                    CAsset v_asset = new CAsset();
                    v_asset = v_objCtrlAsset.Get_Asset_By_ID(v_i);
                    m_lsAsset.Add(v_asset);
                    lsAsset.Items.Add(v_asset.Name);
                    v_dbTotal_Manday += v_asset.Manday;
                }

                txtTotalManday.Text = "Total Manday :" + v_dbTotal_Manday.ToString();

                //load toàn bộ danh sách các artist thực hiện:
                CProject_Staff_Controller v_objCtrlStaff = new CProject_Staff_Controller();
                m_lsStaff = new List<CProject_Staff>();
                m_lsStaff = v_objCtrlStaff.A001_List_Project_Staff_By_Project_ID_And_Regency_ID(CConfig.g_intGem_Tools_Current_Project ,CUtility.Convert_To_Int32(EStaff_Type.Artist));
                //m_lsStaff = v_objCtrlStaff.A
                lkeArtist.Properties.DataSource = m_lsStaff;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);;
            }
        }

        string Check_Data()
        {
            StringBuilder v_sb = new StringBuilder();
            if (CUtility.Convert_To_Int32(lkeArtist.EditValue) == 0)
            {
                v_sb.AppendLine("-Missing artist.");
            }

            return v_sb.ToString();
        }

        private void Assign_Control_To_Obj(ref CResources pTasks, CAsset pAsset, string pstrTaskName, int piParent_Task_ID, double p_dbManday)
        {
            pTasks.Asset_ID = pAsset.Auto_ID;
            pTasks.Staff_ID = CUtility.Convert_To_Int32(lkeArtist.EditValue);
            pTasks.Status_ID = CUtility.Convert_To_Int32(ETasks_Status.None);
            pTasks.Manday = p_dbManday;
            pTasks.Project_ID = pAsset.Project_ID;
            pTasks.Parent_ID = piParent_Task_ID;
            pTasks.Name = pstrTaskName;
            pTasks.Start_Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 9, 0, 0, 0);
            pTasks.End_Date = pTasks.Start_Date.AddDays(p_dbManday);
            pTasks.End_Date = new DateTime(pTasks.End_Date.Year, pTasks.End_Date.Month, pTasks.End_Date.Day, 23, 59, 59, 0);
        }

        /// <summary>
        /// khi Assign người thì kiểm tra có bao nhiêu Asset đc Assign ? 
        /// nếu số lượng Asset = 1 ->  Chỉ cần tạo ra 1 task thôi.
        /// Nếu số lượng Asset >= 2 -> Tạo ra 1 tasks cha, mỗi task cha cho toàn bộ các Asset này -  cac Asset kia sẽ được tạo thành Tasks con -> Parent ID = Parent ID Tasks cha.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAssign_Click(object sender, EventArgs e)
        {
            try
            {
                string error = Check_Data();
                if (error != "")
                {
                    throw new Exception(error);
                }

                double v_dbManday = 0; 
                //lấy danh sách Asset được Assign:
                CResources_Controller v_objCtrlTask = new CResources_Controller();
                string v_strTaskName = lkeArtist.Text + ": ";
                foreach (CAsset v_obj in m_lsAsset)
                {
                    v_dbManday += v_obj.Manday;
                    v_strTaskName += v_obj.Name + "; ";
                }

                //trước tiên kiểm tra xem có bao nhiêu Asset đc assign :
                // nếu số lượng Asset = 1 ->  Chỉ cần tạo ra 1 task thôi.
                CResources v_Tasks = new CResources();
                Assign_Control_To_Obj(ref v_Tasks, m_lsAsset[0], v_strTaskName, 0, v_dbManday);
                v_Tasks.Auto_ID = v_objCtrlTask.Insert_Resources(v_Tasks);

                CTasks_Detail_Controller v_objCtrlTasks_Detail = new CTasks_Detail_Controller();
                CTasks_Detail v_TasksDetail = new CTasks_Detail();
                v_TasksDetail.Tasks_ID = v_Tasks.Auto_ID;
                v_TasksDetail.Date = DateTime.Now;
                v_TasksDetail.Status_ID = CUtility.Convert_To_Int32(ETasks_Status.WIP);
                v_objCtrlTasks_Detail.Insert_Tasks_Detail(v_TasksDetail);

                if(lsAsset.ItemCount >= 2) // ở trên đã kiểm tra ls rỗng rồi, nên ở đây mặc định là >= 2
                {
                    foreach (CAsset v_obj in m_lsAsset)
                    {
                        CResources v_subTasks = new CResources();
                        Assign_Control_To_Obj(ref v_subTasks, v_obj, v_obj.Name, v_Tasks.Auto_ID, v_obj.Manday);
                        v_subTasks.Auto_ID = v_objCtrlTask.Insert_Resources(v_subTasks);
                    }
                }
                this.Close();

                XtraMessageBox.Show("Assign Success !");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);;
            }
        }
    }
}
